/**
 * @module ChiVoxSDK
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import cfg from './config.json';
import Utils from './utils';

import Html5Recorder from './html5/html5recorder';
import FlashRecorder from './flash/flashrecorder';

/**
 * 初始化录音机实例的默认值。<br/>
 *
 * @name Default_ChiVoxRecorder_Options
 * @global
 * @constant
 * @property {enum=} mode - 指定录音机的模式。可以指定为ChiVoxSDK.MODE.HTML5(默认值) 或 ChiVoxSDK.MODE.FLASH。
 * @property {string=} server - 指定SDK的评分服务器地址。默认值: ws://cloud.chivox.com
 * @property {string=} bak_server - 指定SDK的评分备份服务器地址。默认值: ''
 * @property {string} appKey - 指定录音机使用的appKey（由驰声提供）。**必填**
 * @property {string} sigurl - 指定录音机获取sig和timestamp的服务器地址（sig算法由驰声提供）。**必填**
 * @property {string=} recorderid - 指定录音机渲染在当前页面的DOM节点ID。默认值: chivox-recorder
 * @property {Object} data - 参数解释详见：{@link Default_ChiVoxSDK_Options}
 * @property {int} width - 录音机的波形图宽度, 默认值220px。
 * @property {int} height - 录音机的波形图高度, 默认值140px。
 * @property {boolean} showFlash - Flash模式下是否显示Flash。默认：true
 * @property {string} backgroundColor - Flash模式下显示Flash的背景, 默认："0x00fffff"。(已过期)
 * @property {string} wmode - Flash模式下显示Flash的的模式。默认值："transparent"
 * @property {callback} onInit - 录音机初始化完成后的Callback调用。格式：(code, message) => {}
 * @property {callback} onError - 录音机初始化或运行期间出错时的Callback调用。格式：(error) => {}
 * @property {callback} onConnectorStatusChange - 录音机与服务器的连接状态发生变化时的Callback调用。格式：(code, message) => {}
 * @property {callback} onMicStatusChange - Flash模式下，麦克风状态发生变化时的Callback调用。格式：() => {}
 */
const _default_options = {
    recorderid:  'chivox-recorder',
    appKey:      '',
    server:      cfg.server,
    bak_server:  null,
    sigurl:      '',
    data:        null,
    mode:        cfg.mode.HTML5,
    showFlash:   true,
    backgroundColor: '0x00fffff',
    width:         220,
    height:        140,
    wmode:         'transparent',
    onInit:      function(errno, err) {},
    onError:     null,
    onConnectorStatusChange: function() {},
    onMicStatusChange: function() {},
};

/**
 * RecorderBuilder类。<br/><br/>
 *
 * 根据mode构建指定模式的录音机。
 */

class RecorderBuilder {
    /**
    * 根据mode构建指定模式的录音机。
     *
     * @param {Object} options - 初始化参数。格式参见：{@link Default_ChiVoxRecorder_Options}
     * @return {Object} - 指定模式的录音机实例。(Html5Recorder或FlashRecorder的实例)
     */
    static build(options) {
        var _curr_recorder;

        options = Utils.extend(options, _default_options, true);
        _curr_recorder = (options.mode == cfg.mode.HTML5) ? new Html5Recorder(options) : new FlashRecorder(options);

        return _curr_recorder;
    }
}

export default RecorderBuilder
